package exp5.实验三;

import ArithMathOperation.SuffixExpression;

import javax.crypto.Cipher;
import java.io.*;
import java.net.Socket;
import java.security.Key;
import java.util.Scanner;

public class SocketClient3 {
    public static void main(String[] args) throws Exception {

        Socket socket = new Socket("172.16.43.167",8800);
        Scanner scan = new Scanner(System.in);

        OutputStream outputStream = socket.getOutputStream();

        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);

        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));

        System.out.print("请输入中缀表达式：");
        String str1 = scan.nextLine();
        SuffixExpression str2 = new SuffixExpression();
        String houzhui = str2.SuffixExpression(str1);

        FileInputStream f=new FileInputStream("key1.dat");
        ObjectInputStream b=new ObjectInputStream(f);
        Key k=(Key)b.readObject( );
        Cipher cp=Cipher.getInstance("DESede");
        cp.init(Cipher.ENCRYPT_MODE, k);
        byte ptext[]=houzhui.getBytes("UTF8");
        System.out.print("字节数组编码:");
        for(int i=0;i<ptext.length;i++){
            System.out.print(ptext[i]+",");
        }
        System.out.println("\n" + "加密内容");
        byte ctext[]=cp.doFinal(ptext);
        for(int i=0;i<ctext.length;i++){
            System.out.print(ctext[i] +",");
        }

        outputStream.write(ctext);
        outputStream.flush();
        socket.shutdownOutput();
        System.out.println();
        String reply = null;
        while (!((reply = bufferedReader.readLine()) ==null)){
            System.out.println("服务器运算结果"+ reply);
        }

        bufferedReader.close();
        inputStream.close();
        outputStreamWriter.close();
        //printWriter.close();
        outputStream.close();
        socket.close();
    }
}

